/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.logging;

import java.sql.Types;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlUpdate;

import net.groupme.logging.model.ActionIdentifier;
import net.groupme.model.ObjectFactory;
import net.groupme.model.Resource;
import net.groupme.model.User;
import net.groupme.model.dao.DATABASE_CONSTANTS;

/**
 * created on Sep 20, 2007
 * 
 * This logger writes events directly into an underlying MySQL Database.
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.3 $ $Date: 2007-09-28 21:10:26 $
 */
public class GroupMeLoggerDBImpl implements GroupMeNavigationLogger {
	
	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());
	
	

	/**
	 * Creates a new GroupMeLoggerDBImpl instance. 
	 */
	protected GroupMeLoggerDBImpl() {
		super();
	}

	/* (non-Javadoc)
	 * @see net.groupme.logging.GroupMeNavigationLogger#log(net.groupme.model.User, net.groupme.logging.model.ActionIdentifier)
	 */
	public void log(User user, String sessionID, ActionIdentifier actionID){
		//TODO: store general events
	}
	
	/* (non-Javadoc)
	 * @see net.groupme.logging.GroupMeNavigationLogger#logNavigation(net.groupme.model.User, net.groupme.logging.model.ActionIdentifier, java.lang.String, net.groupme.model.Resource)
	 */
	public void logNavigation(User user, String sessionID, ActionIdentifier actionID, String searchString, Resource browsedResource) {

	}
	/* (non-Javadoc)
	 * @see net.groupme.logging.GroupMeNavigationLogger#logSearch(net.groupme.model.User, net.groupme.logging.model.ActionIdentifier, java.lang.String)
	 */
	public void logSearch(User user, String sessionID, ActionIdentifier actionID, String searchString) {
		SqlUpdate su = new SqlUpdate(
				ObjectFactory.ds,
				"INSERT INTO " + DATABASE_CONSTANTS.TABLE_LOG_EVENT_NAVIGATION //$NON-NLS-1$
						+ " (" + DATABASE_CONSTANTS.LOG_EVENT_NAVIGATION_USER_ID + ", " + DATABASE_CONSTANTS.LOG_EVENT_NAVIGATION_SESSION_ID + ", "+ DATABASE_CONSTANTS.LOG_EVENT_NAVIGATION_ACTION + ", " + DATABASE_CONSTANTS.LOG_EVENT_NAVIGATION_SEARCH_STRING + ") VALUES (?, ?, ?, ?)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 
		su.declareParameter(new SqlParameter("userId", Types.INTEGER)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("sessionID", Types.VARCHAR)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("action", Types.VARCHAR)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("value", Types.VARCHAR)); //$NON-NLS-1$
		su.compile();
		Object[] oa = {(user == null ? null : user.getId()), sessionID, actionID.getIdentifierValue(), searchString};
		int count = su.update(oa);
		if(count == 0){
			logger.info("Logging failed - LogEvent: " + sessionID + ", " + actionID.getIdentifierValue() + ", " + searchString);
		}else{
			logger.info("LogEvent stored: " + sessionID + ", " + actionID.getIdentifierValue() + ", " + searchString);
		}
		
	}
	
	/* (non-Javadoc)
	 * @see net.groupme.logging.GroupMeNavigationLogger#logBrowsing(net.groupme.model.User, net.groupme.logging.model.ActionIdentifier, net.groupme.model.Resource)
	 */
	public void logBrowsing(User user, String sessionID, ActionIdentifier actionID, Resource browsedResource) {
		SqlUpdate su = new SqlUpdate(
				ObjectFactory.ds,
				"INSERT INTO " + DATABASE_CONSTANTS.TABLE_LOG_EVENT_NAVIGATION //$NON-NLS-1$
						+ " (" + DATABASE_CONSTANTS.LOG_EVENT_NAVIGATION_USER_ID + ", " + DATABASE_CONSTANTS.LOG_EVENT_NAVIGATION_SESSION_ID + ", " + DATABASE_CONSTANTS.LOG_EVENT_NAVIGATION_ACTION + ", " + DATABASE_CONSTANTS.LOG_EVENT_NAVIGATION_RESOURCE_ID + ") VALUES (?, ?, ?, ?)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 
		su.declareParameter(new SqlParameter("userId", Types.INTEGER)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("sessionID", Types.VARCHAR)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("action", Types.VARCHAR)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("resourceId", Types.INTEGER)); //$NON-NLS-1$
		su.compile();
		Object[] oa = {(user == null ? null : user.getId()), sessionID, actionID.getIdentifierValue(), browsedResource.getId()};
		int count = su.update(oa);
		if(count == 0){
			logger.info("Logging failed - LogEvent: " + sessionID + ", " + actionID.getIdentifierValue() + ", " + browsedResource.getName());
		}else{
			logger.info("LogEvent stored: " + sessionID + ", " + actionID.getIdentifierValue() + ", " + browsedResource.getName());
		}
	}
}
